﻿using System;
using System.Collections.Generic;
using System.Text;
using Marx.Utils;

namespace Marx
{
    public class Timer
    {
        public int ID;
        public event Action<int> Method;
        public long Delay;
        public long Interval;
        public long Timestamps;

        private bool first;

        public long NextTriggerTime
        {
            get
            {
                if (first)
                {
                    if (Delay == long.MaxValue)
                        return long.MaxValue;
                    return Timestamps + Delay;
                }
                else
                {
                    if (Interval == long.MaxValue)
                        return long.MaxValue;
                    return Timestamps + Interval;
                }
            }
        }

        public bool Expire { get { return NextTriggerTime == long.MaxValue; } }


        public Timer()
        {
            first = true;
        }

        public void Invoke()
        {
            Method.Invoke(ID);
            first = false;
            Timestamps = TimeUtils.CurUnixTimestamp;
        }
    }
}
